﻿using System;
using System.Collections.Generic;
using System.Linq;
using Com.CodePlex.SPC3.SPbAM.View;

namespace Com.CodePlex.SPC3.SPbAM.Service {

	public class CommandParameterDialogService {

		public CommandParameterDialogResult Show(IList<CommandParameterWithType> paras) {
			if (paras == null) {
				throw new ArgumentNullException("paras");
			}
			CommandParameterDialog dialog = new CommandParameterDialog();
			dialog.Owner = System.Windows.Application.Current.MainWindow;
			ViewModel.CommandParameterDialogViewModel model = new ViewModel.CommandParameterDialogViewModel();
			paras.ToList().ForEach(i => {
				model.Parameters.Add(new ViewModel.CommandParameterViewModel(i.Name, i.Type) { Value = i.Value });
			});
			dialog.DataContext = model;
			CommandParameterDialogResult result = new CommandParameterDialogResult();
			if (dialog.ShowDialog() == true) {
				result.Values = new List<CommandParameterWithType>();
				model.Parameters.Select(i => (ViewModel.CommandParameterViewModel)i).ToList().ForEach(i => {
					result.Values.Add(new CommandParameterWithType() { Type = i.Type, Name = i.DisplayName, Value = i.Value });
				});
			}
			return result;
		}
	}

	public class CommandParameterDialogResult {
		private IList<CommandParameterWithType> values;
		public bool IsValid {
			get { return Values != null; }
		}
		public IList<CommandParameterWithType> Values {
			get { return values; }
			set { values = value; }
		}
	}
}